import { formatTime } from "../../../utils/util";
import { uploadPic,createGame } from "../../../utils/request/api";
import { Competition } from "../../../utils/request/interface";
import { parseFormat } from "../../../miniprogram_npm/@vant/weapp/count-down/utils";
import msg from "../../../utils/msg";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: {
      game_title: "", //比赛名称
      game_time: "", //比赛时间
      game_address: "", //比赛地址
      Signup_date_end: "", //报名截止时间
      local_and_phone: "", //联系电话
      game_player_team: "", //665-单人，666团体
      number_of_member: "", //限报人数
      minimum_team: 1, //每队人数,单人比赛默认为1
      game_money: "", //报名费用
      isSignOnline: "", //收费方式 641-线下收费，642-在线缴费
      game_online: "649", //648-隐藏比赛，649-显示比赛
      intro_content_temp: "", //赛事介绍内容
      game_small_pic: "", //赛事缩略图 2024/04/11/1712803327254_0.png
      game_big_pic: "", //赛事详情滚动图，多图用","隔开
    } as Competition,
    //时间选择器用到的时间戳
    currentDate: new Date().getTime() as number,
    pickTime: false as boolean,
    pickTimeName: '' as string,
    fileSmallPic: [] as any,
    fileBigPic: [] as any,
    showSuccess:false as boolean,
  },
  submit:async function(){
    
  },
  // stepChange(e:any){
  // },
  //上传前检查数据
  checkValid() {
    let param:any = this.data.param;
    for(let key in param){
      let temp = param[key];
      if(!temp) {
        msg.show('请输入完整表单');return false;
      }
    }
    return true;
  },
  afterReadSmall: async function (event: any) {
    const { file } = event.detail;
    msg.Loading();
    let res = await uploadPic(file.url, 'Filedata')
      .then((res: any) => { return res; });
    if (res.statusCode != 200) {
      msg.show('服务器维护中','请稍后再试');return;
    }
    let data = res.data;
    if (typeof data == 'string') data = JSON.parse(data);
    let fileName = data.savename;
    let fileList = [];
    fileList.push(file);
    this.setData({
      fileSmallPic: fileList,
      ['param.game_small_pic']: fileName,
    });
    msg.hideLoading();

  },
  //删除图片
  actSmaDel: function () {
    this.setData({
      fileSmallPic: [],
      ['param.game_small_pic']: '',
    });
  },
  afterReadBig:async function (event: any) {
    const { file } = event.detail;
    msg.Loading();
    let res = await uploadPic(file.url, 'Filedata')
      .then((res: any) => { return res; });
    if (res.statusCode != 200) {
      msg.show('服务器维护中','请稍后再试');return;
    }
    let data = res.data;
    if (typeof data == 'string') data = JSON.parse(data);
    let fileName = data.savename;

    let fileBigPic=this.data.fileBigPic;
    let pic = this.data.param.game_big_pic;
    if(pic=='') pic=fileName;
    else pic+=','+fileName;
    fileBigPic.push(file);
    this.setData({
      fileBigPic: fileBigPic,
      ['param.game_big_pic']: pic,
    });
    msg.hideLoading();
  },
  //删除图片
  actBigDel: function (e:any) {
    let index = e.detail.index;
    let fileBigPic = this.data.fileBigPic;
    fileBigPic.splice(index, 1);
    let pics = this.data.param.game_big_pic.split(',');
    pics.splice(index, 1);
    this.setData({
      fileBigPic: fileBigPic,
      ['param.game_big_pic']: String(pics),
    });
  },
  onChange(e: any) {
    this.setData({
      ['param.game_player_team']: e.detail,
    });
    if (e.detail == 665)
      this.setData({
        ['param.minimum_team']: 1,
      });
  },
  onChange2(e: any) {
    this.setData({
      ['param.isSignOnline']: e.detail,
    });
  },
  switchChange(e: any) {
    console.log('e=', e);
    this.setData({
      ['param.game_online']: e.detail ? '649' : '648',
    })

  },
  //
  pickTime(e: any) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      pickTime: true,
      pickTimeName: name,
    });
  },
  confirmTime(e: any) {
    let format_time = formatTime(new Date(e.detail));
    this.setData({
      ['param.' + this.data.pickTimeName]: format_time,
    });
    this.onClose();
  },
  onClose() {
    this.setData({
      pickTime: false,
    })
  },
  bindInput(e: any) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      ['param.' + name]: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})